package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import databeans.Customer;
import databeans.Employee;


import model.CustomerDAO;
import model.EmployeeDAO;
import model.Model;


public class EmployeeCustomerManagerAction extends Action {
    private EmployeeDAO employeeDAO;
    private CustomerDAO customerDAO;
    
    public EmployeeCustomerManagerAction(Model model) {
        employeeDAO = model.getEmployeeDAO();
        customerDAO =  model.getCustomerDAO();    
    }

    public String getName() {
        return "employeeCustomerManager.do";
    }

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
        String loginPage = "employee/employee-login.jsp";
        String dashboardPage = "employee/employee-dashboard.jsp";
        String originPage = "employee/customer-manager.jsp";
        String successPage = originPage; //TODO change to employee dashboard
        
        System.out.println("Start perform - 1");
      //set the employee
        Employee employee = null;
        Customer customer = null;

        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        request.setAttribute("page", "cust-manager");
        
        employee = (Employee) request.getSession(true).getAttribute("employee");
        if (employee == null) {
            errors.add("You need to log in");
            request.setAttribute("page", "login");
            return loginPage ;
        }
        
        try {
			Customer[] customers = customerDAO.getCustomers();
			request.setAttribute("customers", customers);
		} catch (DAOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
        return originPage ;
    }
}